package cn.com.xinjinfu.pdoulcProduct.fragment.MyVoucher;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.alibaba.fastjson.JSON;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import cn.com.xinjinfu.pdoulcProduct.R;
import cn.com.xinjinfu.pdoulcProduct.adapter.MyVoucherListAdaper;
import cn.com.xinjinfu.pdoulcProduct.common.ConstantValues;
import cn.com.xinjinfu.pdoulcProduct.engine.DataHandler;
import cn.com.xinjinfu.pdoulcProduct.entity.MyInvestListBean;
import cn.com.xinjinfu.pdoulcProduct.entity.MyVoucherBean;
import cn.com.xinjinfu.pdoulcProduct.entity.MyVoucherBean.VoucherListA;
import cn.com.xinjinfu.pdoulcProduct.entity.User;
import cn.com.xinjinfu.pdoulcProduct.fragment.BaseFragment;
import cn.com.xinjinfu.pdoulcProduct.manager.AccountManager;
import cn.com.xinjinfu.pdoulcProduct.manager.L;
import cn.com.xinjinfu.pdoulcProduct.manager.ToastManager;
import cn.com.xinjinfu.pdoulcProduct.xlistview.XListView;

/**
 * 投标中列表
 * Created by android-adt on 2015/12/31.
 */
public class HaveUseListFragment extends BaseFragment implements XListView.IXListViewListener, View.OnClickListener {
    private View viewFragment;
    private Handler mHandler;
    private Activity mActivity;
    private XListView xListView = null;
    private MyVoucherListAdaper investAdapter;
    private List<VoucherListA> page;
    private RequestQueue requen;
    private int currPage;
    private LinearLayout no_invest_view;
    private MyVoucherBean myVoucher;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        this.mActivity = activity;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        viewFragment = inflater.inflate(R.layout.my_voucher_list, null);
        requen = Volley.newRequestQueue(mActivity);
        currPage = 1;
        initViews();
        loginRequest(1, currPage);
        return viewFragment;
    }

    private void initViews() {
        mHandler = new Handler();
        no_invest_view = (LinearLayout) viewFragment.findViewById(R.id.no_invest_view);
        xListView = (XListView) viewFragment.findViewById(R.id.xListView);
        xListView.setPullLoadEnable(true);// 是否显示下拉刷新和上啦加载
        xListView.setPullRefreshEnable(true);
        investAdapter = new MyVoucherListAdaper(mActivity);
        xListView.setAdapter(investAdapter);
        xListView.setXListViewListener(this);

    }

    private void loginRequest(int type, int currPage) {
        User user = AccountManager.getInstance().getUser();
        if (user != null) {
            final Map<String, String> parameters = DataHandler.getParameters("1426");
            parameters.put("id", user.getCurrUser().getSign());
            parameters.put("type", type + "");
            parameters.put("currPage", currPage + "");
            JsonObjectRequest request = new JsonObjectRequest(DataHandler.getBuildUrl(parameters), null, succeedRespon, null);
            request.setRetryPolicy(new DefaultRetryPolicy(ConstantValues.TIMEOUT, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
            requen.add(request);
        }
    }

    private Response.Listener<JSONObject> succeedRespon = new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject jobj) {
            L.d("代金券jobj" + jobj.toString());
            myVoucher = JSON.parseObject(jobj.toString(), MyVoucherBean.class);
            List<VoucherListA> pages = myVoucher.getVoucherListA();
            page = new ArrayList<>();
            for (VoucherListA item:pages){
                item.setUseType(1);
                page.add(item);
            }
            if (currPage == 1) {
                if (page != null && !page.isEmpty()) {
                    no_invest_view.setVisibility(View.GONE);
                    investAdapter.setList(page);
                } else {
                    no_invest_view.setVisibility(View.VISIBLE);
                }
            } else {
                if (page != null && !page.isEmpty()) {
                    investAdapter.addList(page);
                } else {
                    ToastManager.showShort(mActivity, "没有更多数据");
                }
            }
            onLoad();
        }
    };

    private void onLoad() {
        xListView.stopRefresh();
        xListView.stopLoadMore();
        xListView.setRefreshTime("刚刚");
    }

    @Override
    public void onRefresh() {
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                currPage = 1;
                loginRequest(1, currPage);
            }
        }, 1000);
    }

    @Override
    public void onLoadMore() {
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                currPage++;
                loginRequest(1, currPage);
            }
        }, 1000);
    }

    @Override
    protected void lazyLoad() {

    }


    @Override
    public String getFragmentName() {
        return null;
    }

    @Override
    public void onClick(View v) {

    }
}
